package Z.dailyExercise._0224;

import java.util.Stack;

public class _832_翻转图像 {
    /**
     * 自解：借助栈进行实现
     * @param A
     * @return
     */

    public int[][] flipAndInvertImage(int[][] A) {

        Stack<Integer> stack = new Stack<>();
        int len = A[0].length;
        for (int i=0;i<A.length;i++){
            //int[] arr = A[i];
            for (int j=0;j<len;j++){
                stack.push(A[i][j] == 1?0:1);
            }
            int index=0;
            for (int j=0;j<len;j++){
                //stack.push(A[i][j] == 1?0:1);
                A[i][j]=stack.pop();
            }

        }

        return A;
    }

    /**
     * 另一解决：每一行当中 借助双指针进行交换
     * @param A
     * @return
     */
    public int[][] flipAndInvertImage1(int[][] A) {
        int len = A[0].length;
        for (int i=0;i<A.length;i++){
            int l=0,r=len-1;
            while (l<=r){
                //不想等就不变 相等就全换
                if (A[i][l]==A[i][r]){
                    A[i][l] = Math.abs(A[i][l]-1);
                    A[i][r] = A[i][l];
                }
                l++;
                r--;
            }
        }

        return A;
    }


}
